

import ctypes

# 定义一个类似C语言联合体的结构体
class UnionExample(ctypes.Union):
    _fields_ = [("int_value", ctypes.c_int32),
                ("float_value", ctypes.c_float)]
    
class UInt32ToInt32(ctypes.Union):
    _fields_ = [("uint_value", ctypes.c_uint32),
                ("int_value", ctypes.c_int32)]

write_txt = open('C:\\Users\\zhangzhengbo\\Desktop\\pos\\result.txt', 'w')
src_txt = open('C:\\Users\\zhangzhengbo\\Desktop\\pos\\src1.txt', 'w')

# 2轴
# cmd_index = 726    78
cmd_index = 648
# int_index = 786
int_index = 708
# cmd_index = 156    72
rev_index = 84

# 创建一个实例
# u = UnionExample()

# u.int_value = 12345
# print(u.int_value)
# print(u.float_value)

def get_data(line, index):
    # 获取使能命令 7或者f
    # cmd = line.strip()[index+3 : index+5] + line.strip()[index : index+2]
    # 打印特定位置的数据对象
    # print(cmd)


    d1 = line.strip()[index+6:index+8]
    d2 = line.strip()[index+9:index+11]
    d3 = line.strip()[index+12:index+14]
    d4 = line.strip()[index+15:index+17]
    str_d = d4+d3+d2+d1
    # print("{}:{}".format(cmd, str_d))
    hex_int = int(str_d, 16)
    # return "{},{}".format(str(int(cmd,16)),str(hex_int))
    u = UInt32ToInt32()
    u.uint_value = hex_int
    return str(u.int_value)
    # return str(hex_int)

def get_intpart(line, index):
    # 获取使能命令 7或者f
    # cmd = line.strip()[index+3 : index+5] + line.strip()[index : index+2]
    # 打印特定位置的数据对象
    # print(cmd)
    u = UnionExample()
    d0 = line.strip()[index+0:index+2]
    d1 = line.strip()[index+3:index+5]
    d2 = line.strip()[index+6:index+8]
    d3 = line.strip()[index+9:index+11]
    str_d = d3+d2+d1+d0
    hex_int = int(str_d, 16)
    u.int_value = hex_int
    float_int = u.float_value
    return str(float_int)


def get_float(int_txt, float_txt):
    a1 = float(int_txt)
    a2 = float(float_txt)
    return str(a1+a2)

# 打开文本文件
with open('C:\\Users\\zhangzhengbo\\Desktop\\pos\\wireshark.txt', 'r') as file:
    # 逐行读取文件内容
    for line in file:
        # 获取字符串长度
        length = len(line.strip())
        if length >= 200:
            src = ""
            for i in range(6):
                index = cmd_index + i * 78
                src = src + get_data(line,index) + ","
            for i in range(6):
                index = rev_index + i * 72
                src = src + get_data(line,index) + ","
            src = src + "0\n"
            # t1 = get_data(line,cmd_index)
            # t2 = get_intpart(line,int_index)
            # t2 = get_data(line,rev_index)
            # write_txt.writelines("{},{},{}\n".format(t1,t2,get_float(t1,t2)))
            write_txt.writelines(src)
            # src_txt.writelines(line)
